﻿using System;
using System.IO;
using System.Web.Mvc;

namespace LeanBoard.Controllers
{
    public class ProfileImageController : Controller
    {
        private const string ProfileImagesPath = "/Client/Profiles/";
        private const string CountriesImagesPath = "/Client/Countries/";
        //
        // GET: /ProfileImage/
        [OutputCache(Duration = 5000, VaryByParam = "imageProfile")]
        public FileContentResult GetImage(string imageProfile)
        {
            string fileName = AppDomain.CurrentDomain.BaseDirectory + ProfileImagesPath + imageProfile;
            if (!System.IO.File.Exists(fileName))
            {
                fileName = AppDomain.CurrentDomain.BaseDirectory + ProfileImagesPath + "Anonymous.jpg";
            }
            byte[] buff = null;
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(fileName).Length;
            buff = br.ReadBytes((int)numBytes);
            return new FileContentResult(buff, "image/jpeg");
        }

        //
        // GET: /ProfileImage/
        [OutputCache(Duration = 5000, VaryByParam = "countryName")]
        public FileContentResult GetCountryImage(string countryName)
        {
            string fileName = AppDomain.CurrentDomain.BaseDirectory + CountriesImagesPath + countryName;
            if (!System.IO.File.Exists(fileName))
            {
                return null;
            }
            byte[] buff = null;
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(fileName).Length;
            buff = br.ReadBytes((int)numBytes);
            return new FileContentResult(buff, "image/png");
        }

    }
}
